package testClasses.misc;

import galaxies.enums.GalaxySize;
import gameAppLayer.XMLFileBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import stars.StarController;
import stars.StarModel;
import stars.StarModelBuilder;
import stars.StarViewBuilder;
import stars.enums.EStarType;
import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.MVCMaker;
import view.EViewList;
import view.View;

import com.thoughtworks.xstream.XStream;

/*
 * Used for testing XStream's capabilities re: object storage, re-creation from XML.
 */
public class XMLxstreamSandbox extends ATest {

	Random rand = new Random();
	
	public XMLxstreamSandbox(TestingSystem system) {
		super(system);
		this.name = "XMLxstreamSandbox";
	}

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 */
//	public static void main(String[] args) throws IOException, ClassNotFoundException {
//		XMLxstreamSandbox fb = new XMLxstreamSandbox();
//		fb.buildXMLFiles();
//
//	}
	
	private double randomTemperature(EStarType type) {		
		double maxTemp = type.getMaxTemp();
		double minTemp = type.getMinTemp();
		double tempRange = maxTemp - minTemp;
		double temperature = rand.nextDouble()*tempRange + minTemp;
		
		return temperature;		
	}

	@Override
	public boolean execute() throws IOException, ClassNotFoundException {
		
		EStarType type = EStarType.MS_ORANGE;
		
		StarModelBuilder modelBuild = new StarModelBuilder();
		StarViewBuilder viewBuild = new StarViewBuilder();
		
		double temp = randomTemperature(type);		
		
		StarModel model = modelBuild.createMStar(temp, EStarType.MS_ORANGE);
		HashMap<EViewList, View> views = viewBuild.buildViews(assetManager);
		StarController controller = new StarController(eventManager);
		
		MVCMaker.linkElements(model, views, controller);
		
		XStream xstream = new XStream();
		xstream.alias("star", StarController.class);
		
		File file = new File("C://Documents and Settings//rmeyers//workspace//t.txt");	
		
//		if (!file.exists()) {
//			file.createNewFile();
//		}
//		else {
//			
//		}
		
		//Output to file
		FileOutputStream fos = new FileOutputStream(file);
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		ObjectOutputStream out = xstream.createObjectOutputStream(osw);
		
		out.writeObject(controller);
		out.close();
		
//		//Input from file
//		FileInputStream fis = new FileInputStream(file);
//		InputStreamReader isw = new InputStreamReader(fis);
//		ObjectInputStream in = xstream.createObjectInputStream(isw);
//		
//		GalaxySize gs1 = (GalaxySize) in.readObject();
//		
//		for (Map.Entry<String, Integer> entry : gs1.getGalaxySizes().entrySet()) {
//			System.out.println(entry.getKey());
//			System.out.println(entry.getValue());
//		}
		
		return false;
	}

}
